const router = require('koa-router')()
const JWT = require('jsonwebtoken')
const userList = [{
  "username": "admin",
  "password": "admin",
  "nickname": "管理员",
  "avatar": "https://robohash.org/121231",
  "role": 'admin'
},{
  "username": "zhangsan",
  "password": "zhangsan",
  "nickname": "小三三",
  "avatar": "https://robohash.org/123?set=set4",
  "role": 'user'
},{
  "username": "lisi",
  "password": "lisi",
  "nickname": "有梦想的李四",
  "avatar": "https://robohash.org/123?set=set3",
  "role": 'user'
}]

router.post('/login', ctx => {
  const { username, password } = ctx.request.body

  const user = userList.find(v => v.username === username && v.password === password);

  if(user) {
    ctx.body = {
      code: 200,
      message: '登录成功',
      data: {
        userInfo: user,
        token: JWT.sign(user, '2345fw23qr2r3')
      }
    }
  } else {
    ctx.body = {
      code: 400,
      message: '登录失败'
    }
  }
})

module.exports = router
